本篇筆記整理自 Google Cloud 網路基礎實驗,重點在於透過 Google Cloud SDK (gcloud CLI) 理解 VPC 網路架構、自動模式子網(Auto mode subnets)、防火牆規則機制,以及 Compute Engine VM 實例之間的網路連線性。
10.128.0.0/9 區段內)。每個專案預設都有一個 default 網路與四條基本防火牆規則。本步驟示範檢視預設資源、清空防火牆、刪除預設 VPC,並驗證「失去 VPC 後無法建立 VM」的情境。
# 查看 default 網路下的子網清單
gcloud compute networks subnets list --network=default
# 查看目前網路路由
gcloud compute routes list --filter="network:default"
# 查看 default 網路的防火牆規則
gcloud compute firewall-rules list --filter="network:default"
注意:VPC 內若還有自訂/預設防火牆規則,直接刪除 VPC 會被阻擋,因此必須先刪除所有關聯的防火牆規則。
# 刪除 default 網路內建的四條防火牆規則
gcloud compute firewall-rules delete \
default-allow-icmp \
default-allow-internal \
default-allow-rdp \
default-allow-ssh \
--quiet
# 刪除 default VPC 網路
gcloud compute networks delete default --quiet
# 嘗試在無網路環境下建立 VM(預期會報錯:No more networks / network interface error)
gcloud compute instances create test-vm --zone=us-east1-c --machine-type=e2-micro
重建一個名為 mynetwork 的 Auto mode VPC,手動補齊四條標準防火牆規則,並分別在 us-east1 與 asia-south1 兩個地區各建立一台 e2-micro VM。
# --subnet-mode=auto 會自動在所有地區建立子網路
gcloud compute networks create mynetwork --subnet-mode=auto
# 1. 允許任何來源 ping 本網路實例 (ICMP)
gcloud compute firewall-rules create mynetwork-allow-icmp \
--network=mynetwork \
--action=ALLOW \
--rules=icmp \
--source-ranges=0.0.0.0/0 \
--description="Allow ICMP from anywhere"
# 2. 允許 VPC 內部所有子網互相通訊 (全部協定)
# 10.128.0.0/9 涵蓋所有 Auto mode 預設指派的內部子網網段
gcloud compute firewall-rules create mynetwork-allow-custom \
--network=mynetwork \
--action=ALLOW \
--rules=all \
--source-ranges=10.128.0.0/9 \
--description="Allow internal traffic on the VPC"
# 3. 允許 RDP 遠端桌面 (tcp:3389)
gcloud compute firewall-rules create mynetwork-allow-rdp \
--network=mynetwork \
--action=ALLOW \
--rules=tcp:3389 \
--source-ranges=0.0.0.0/0 \
--description="Allow RDP from anywhere"
# 4. 允許 SSH 連線 (tcp:22)
gcloud compute firewall-rules create mynetwork-allow-ssh \
--network=mynetwork \
--action=ALLOW \
--rules=tcp:22 \
--source-ranges=0.0.0.0/0 \
--description="Allow SSH from anywhere"
# 建立美國東部 VM (mynet-us-vm)
gcloud compute instances create mynet-us-vm \
--zone=us-east1-c \
--machine-type=e2-micro \
--network=mynetwork
# 建立印度南部 VM (mynet-r2-vm)
gcloud compute instances create mynet-r2-vm \
--zone=asia-south1-b \
--machine-type=e2-micro \
--network=mynetwork
藉由查詢 IP、連入 VM 互相 ping,並逐步刪除防火牆規則,觀察封包遺失(packet loss)現象以驗證各規則的具體作用。
# 列出實例名稱、內部 IP (INTERNAL_IP) 與外部 IP (EXTERNAL_IP)
gcloud compute instances list
# SSH 連線到 mynet-us-vm
gcloud compute ssh mynet-us-vm --zone=us-east1-c
在 mynet-us-vm 的終端機內執行:
# 測試連線到 mynet-r2-vm 的「內部 IP」(由 mynetwork-allow-custom 規則放行)
ping -c 3 <mynet-r2-vm-INTERNAL-IP>
# 測試連線到 mynet-r2-vm 的「外部 IP」(由 mynetwork-allow-icmp 規則放行)
ping -c 3 <mynet-r2-vm-EXTERNAL-IP>
# 離開 SSH 終端機
exit
課堂問答重點:
- 題目:Which firewall rule allows the ping to mynet-r2-vm's external IP address?
- 答案:
mynetwork-allow-icmp(因為 external IP 走公開網際網路路由進來,來源不屬於10.128.0.0/9,只能由允許0.0.0.0/0的 ICMP 規則放行)。
gcloud compute firewall-rules delete mynetwork-allow-icmp --quiet
mynet-us-vm,分別 ping 內部 IP 與外部 IP。mynetwork-allow-custom 仍允許 VPC 內所有協定)。deny-all-ingress)。gcloud compute firewall-rules delete mynetwork-allow-custom --quiet
mynet-us-vm 內 ping 內部 IP。# 先確認已退出 VM 終端機,回到 Cloud Shell 執行
gcloud compute firewall-rules delete mynetwork-allow-ssh --quiet
gcloud compute ssh mynet-us-vm --zone=us-east1-c
Connection failed / timeout),因為 TCP Port 22 的傳入規則已不存在。| 操作類別 | gcloud 指令 | 說明 |
|---|---|---|
| VPC 建立 | gcloud compute networks create <NAME> --subnet-mode=auto |
建立自動模式 VPC 網路 |
| VPC 刪除 | gcloud compute networks delete <NAME> |
刪除指定 VPC |
| 建立防火牆 | gcloud compute firewall-rules create <NAME> --network=<NET> --action=ALLOW --rules=<RULES> [--source-ranges=<CIDR>] |
新增傳入允許規則 |
| 刪除防火牆 | gcloud compute firewall-rules delete <NAME> |
移除指定防火牆規則 |
| 建立 VM | gcloud compute instances create <NAME> --zone=<ZONE> --machine-type=<TYPE> --network=<NET> |
在指定區域與 VPC 內建立 VM |
| 查看 VM | gcloud compute instances list |
顯示所有 VM 及其內外部 IP |
| SSH 連線 | gcloud compute ssh <NAME> --zone=<ZONE> |
自動處理金鑰並登入實例 |